﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Domain.Model.Abstract;

using NHibernate.Validator.Constraints;

namespace Domain.Model
{
    public class Subproject : ProjectItem
    {
        #region Properties

        public virtual IList<SubprojectDetail> Details { get; set; }

        public virtual IList<SubprojectTask> Tasks { get; set; }

        [NotNull]
        public virtual SubprojectStatus Status { get; set; } 

        [NotNull]
        public virtual Project Project { get; set; }

        #endregion

        #region Constructor

        public Subproject(string name, string description) : base(name, description)
        {
            Tasks = new List<SubprojectTask>();
            Details = new List<SubprojectDetail>();
            Status = new SubprojectStatus();
        }

        public Subproject()
            : this("new subproject", "this is a new subproject")
        { 
        
        }

        #endregion

        #region Public Interface

        public virtual void AddTask(SubprojectTask task)
        {
            task.Subproject = this;
            Tasks.Add(task);
        }

        public virtual void UpdateTask(SubprojectTask task)
        {
            SubprojectTask match = null;

            foreach (var subprojectTask in Tasks)
            {
                if (subprojectTask.Id == task.Id)
                {
                    match = subprojectTask;
                }
            }

            if (match != null)
            {
                match.Name = task.Name;
                match.Description = task.Description;
                match.Completed = task.Completed;
                match.Subproject = this;
            }
        }

        public virtual void RemoveTask(SubprojectTask task)
        {
            task.Subproject = null;
            Tasks.Remove(task);
        }

        public virtual void AddDetail(string name)
        {
            var detail = GetDetailByName(name);

            if (detail == null)
            {
                detail = new SubprojectDetail(name, "empty");
                detail.Subproject = this;
                Details.Add(detail);
            }
        }

        public virtual void RemoveDetail(string name)
        {
            var detail = GetDetailByName(name);

            if (detail != null)
            {
                detail.Subproject = null;
                Details.Remove(detail);
            }
        }

        public virtual SubprojectDetail GetDetailByName(string name)
        {
            SubprojectDetail result = null;

            foreach (SubprojectDetail detail in Details)
            {
                if (detail.Name.Equals(name))
                {
                    result = detail;
                }
            }

            return result;
        }

        public virtual int GetDetailIndex(string name)
        {
            int index = -1;

            for (var i = 0; i < Details.Count; i++)
            {
                if (Details[i].Name.Equals(name))
                {
                    index = i;
                }
            }

            return index;
        }

        #endregion
    }
}